/**
 * 使用ThreadPoolExecutor创建一个忽略最新任务的线程池，创建规则：
 * 1.核心线程数为5
 * 2.最大线程数为10
 * 3.任务队列为100
 * 4.拒绝策略为忽略最新任务
 */
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ThreadPoolDemo12 {
    public static void main(String[] args) {
        ThreadFactory factory=new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread thread=new Thread(r);
                return thread;
            }
        };
        ThreadPoolExecutor executor=new ThreadPoolExecutor(5,10,10,
                TimeUnit.SECONDS,new LinkedBlockingDeque<>(100),
                factory,new ThreadPoolExecutor.DiscardPolicy());
        for (int i = 0; i < 16; i++) {
            int finalI=i;
            executor.submit(()->{
                try {
                    Thread.sleep(100*finalI);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"执行任务:"+finalI);
            });
        }
    }
}

